package com.aispeech.astools.view;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.aispeech.AIAudioRecord;
import com.aispeech.astools.R;
import com.aispeech.astools.utils.AudioFileWrite;

import java.io.File;

import androidx.appcompat.app.AppCompatActivity;

public class AIJniRecordTestActivity extends AppCompatActivity implements View.OnClickListener {

    private static final String TAG = "AIJniRecordTestActivity";
    private Button mBtnStart, mBtnStop, mBtnRead, mBtnSetup;
    private TextView mTvRec;
    private boolean isRead = false;
    private AudioFileWrite mAudioFileWrite;

    static {
        try {
            System.loadLibrary("aispeechaudio");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_aijni_record_test);

        mBtnStart = findViewById(R.id.btn_start_rec);
        mBtnStop = findViewById(R.id.btn_stop_rec);
        mBtnSetup = findViewById(R.id.btn_setup_rec);
        mBtnRead = findViewById(R.id.btn_read_rec);

        mTvRec = findViewById(R.id.tv_rec);

        mBtnStart.setOnClickListener(this);
        mBtnStop.setOnClickListener(this);
        mBtnSetup.setOnClickListener(this);
        mBtnRead.setOnClickListener(this);

        mAudioFileWrite = new AudioFileWrite(getApplication());
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (isRead) {
            isRead = false;
        }
    }

    @Override
    public void onClick(View v) {
        if (v == mBtnStart) {

            if (isRead) {
                Log.w(TAG, "onClick: already start");
                return;
            }

            int startRec = AIAudioRecord._native_start();
            Log.d(TAG, "onClick: start record : " + startRec);
            mTvRec.append("start record : " + startRec);

            isRead = true;
            String bufPath = getCacheDir().getAbsolutePath() + File.separator + System.currentTimeMillis() + ".pcm";
            mTvRec.append(bufPath);
            mAudioFileWrite.startWrite(bufPath);
        } else if (v == mBtnStop) {
            isRead = false;
        } else if (v == mBtnRead) {
            mTvRec.append("read record data");
            startReadBuf();
        } else if (v == mBtnSetup) {
            int setupRec = AIAudioRecord._native_setup(6, 16000, 4);
            Log.d(TAG, "onClick: setup record : " + setupRec);
            mTvRec.append("setup record : " + setupRec);
        }
    }

    private void startReadBuf() {
        Log.d(TAG, "startReadBuf: start -----");

        new Thread(new Runnable() {
            @Override
            public void run() {
                byte[] buf = new byte[1600];
                int readRec;
                while (isRead) {
                    readRec = AIAudioRecord._native_read_in_byte_array(buf, 0, 1600);
                    if (readRec < buf.length) {
                        break;
                    }
                    mAudioFileWrite.writeBuffer(buf);
                }

                int stopRec = AIAudioRecord._native_stop();
                Log.d(TAG, "stop record : " + stopRec);

                if (null != mAudioFileWrite) {
                    mAudioFileWrite.closeWrite();
                }
            }
        }).start();

        Log.d(TAG, "startReadBuf: end  -----");
    }

}
